package com.test.springboot.utils;

import org.omg.CORBA.PRIVATE_MEMBER;

import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

public class S36 {
    private final  static Random random = new Random();
    static class searchTask implements Runnable{
        private Integer id;
        private CountDownLatch countDownLatch;

        public searchTask(Integer id, CountDownLatch countDownLatch) {
            this.id = id;
            this.countDownLatch = countDownLatch;
        }

        @Override
        public void run() {
            System.out.println("开始寻找第"+id+"颗龙珠");
            int second = random.nextInt(10);
            try {
                Thread.sleep(second*1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("花了"+second+"秒找到第"+id+"龙珠");
            countDownLatch.countDown();

        }
    }

    public static void main(String[] args) {
        List<Integer> idList = Arrays.asList(1,2,3,4,5,6,7);
        CountDownLatch countDownLatch = new CountDownLatch(idList.size());
        for(Integer id:idList) {
            Thread thread = new Thread(new searchTask(id,countDownLatch));
            thread.start();
        }
        try {
            countDownLatch.await(10,TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("开始召唤神龙");

    }
}
